 <!-- 
 @Author: Havery   
 @Date: 2021-01-11 13:58:16   
 @function:应用管理修改
----------------------------------------------------------  
 @Last Modified by:   LC-2021-01-11 13:58:16  
 @Modified function:xxx 
 -->
<template>
  <div>
    <!-- ref相当于Id:model是表单内容-->
    <el-form
      label-width="120px"
      ref="modifyInfo"
      :rules="formInfoRules"
      :model="formInfo"
    >
      <el-row>
        <!-- 基本信息 -->
        <el-col :span="24">
          <el-form-item label="应用名称：" prop="applicatian_name">
            <el-input
              size="mini"
              v-model="formInfo.applicatian_name"
              placeholder="应用名称"
            ></el-input>
          </el-form-item>
          <el-form-item label="应用登录名：" prop="accountid">
            <el-input
              size="mini"
              v-model="formInfo.accountid"
              placeholder="应用登录名"
            ></el-input>
          </el-form-item>
          <el-form-item label="应用类型：" prop="applicatian_type">
            <el-select
              v-model="formInfo.applicatian_type"
              size="mini"
              style="width: 100%"
            >
              <el-option
                v-for="item in listTypeData"
                :key="item.Value"
                :label="item.Text"
                :value="item.Value"
              >
              </el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="应用秘钥：" prop="appsecret">
            <el-input
              size="mini"
              v-model="formInfo.appsecret"
              placeholder="应用秘钥"
            ></el-input>
          </el-form-item>
          <el-form-item label="联合ID：" prop="unionid">
            <el-input
              size="mini"
              v-model="formInfo.unionid"
              placeholder="联合ID"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer " class="el-dialog__footer">
      <el-button size="mini" @click="closeDialog()">取消</el-button>
      <el-button size="mini" type="primary" @click="submitForm('modifyInfo')"
        >保存</el-button
      >
    </div>
  </div>
</template>
<script>
import ApplicationInfoHttp from "@/api/safe/applicationinfo"; //http

//定义变量
const {
  loadApplicationTypeList,
  addSysApplication,
  modifySysApplication,
} = ApplicationInfoHttp;
export default {
  //组件实例的作用域是孤立的。这意味着不能 (也不应该) 在子组件的模板内直接引用父组件的数据。要让子组件使用父组件的数据，需要通过子组件的 props 选项
  //在模板中，要动态地绑定父组件的数据到子模板的 props，与绑定到任何普通的HTML特性相类似，就是用 v-bind。每当父组件的数据变化时，该变化也会传导给子组件
  props: ["formInfo"], //父界面v-bind:editInfo一致
  components: {},
  data() {
    return {
      //应用类型
      listTypeData: [],
      //验证对象，由Rules结尾
      formInfoRules: {
        applicatian_name: [
          {
            required: true,
            min: 1,
            max: 32,
            message: "应用名称不能为空",
            trigger: "blur",
          },
        ],
        accountid: [
          {
            min: 1,
            max: 32,
            required: true,
            message: "应用登录名不能为空",
            trigger: "blur",
          },
        ],
        appsecret: [
          {
             min: 1,
            max: 500,
            required: true,
            message: "应用秘钥不能为空",
            trigger: "blur",
          },
        ],
        unionid: [
          {
            min: 1,
            max: 32,
            required: true,
            message: "联合ID不能为空",
            trigger: "blur",
          },
        ],
      },
    };
  },
  /**html加载完成之前，执行。执行顺序：父组件-子组件 */
  created() {
    this.loadApplicationTypeList();
    if (this.formInfo.applicatian_type != null) {
      this.formInfo.applicatian_type = this.formInfo.applicatian_type + "";
    }
  },
  /**[页面初始化方法]html加载完成后执行。执行顺序：子组件-父组件*/
  mounted() {},
  methods: {
    //关闭增加弹出框
    closeDialog() {
      this.$emit("callBack", null);
    },
    loadApplicationTypeList() {
      loadApplicationTypeList().then((response) => {
        let data = response.data;
        if (data.Code === 1000) {
          this.listTypeData = [];
          for (let index = 0; index < data.Data.length; index++) {
            if (data.Data[index].Value != "") {
              let type = {
                Value: data.Data[index].Value + "",
                Text: data.Data[index].Text + "",
              };
              this.listTypeData.push(type);
            }
          }
        } else {
          this.$messageTip.warning("应用类型获取失败！");
        }
      });
    },
    //保存信息
    submitForm(modifyRef) {
      this.$refs[modifyRef].validate((valid) => {
        if (valid) {
          if (this.formInfo.id != null) {
            modifySysApplication(this.formInfo).then((response) => {
              let data = response.data;
              if (data.Code === 1000) {
                this.$messageTip.success(data.Msg);
                this.$emit("callBack", data);
              } else {
                this.$messageTip.warning(data.Msg);
              }
            });
          } else {
            addSysApplication(this.formInfo).then((response) => {
              let data = response.data;
              if (data.Code === 1000) {
                this.$messageTip.success(data.Msg); 
                this.$emit("callBack", data);
              } else {
                this.$messageTip.warning(data.Msg); 
              }
            });
          }
        } else {
          return false;
        }
      });
    },
  },
};
</script>